home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / handson.exe / STRUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-04  |  3.0 KB  |  110 lines

  1. unit Strutils;
  2. { string handling procedures }
  3. {$IFDEF VER90}
  4. {$H-}
  5. {$ENDIF}
  6. interface
  7.  
  8. const
  9.    separators = ['.', ',', '!', '?',';',':', '"' ];
  10.  
  11. type
  12.    charSet = set of char;
  13.  
  14. procedure firstrestStr( var s, first, rest : string );
  15.    { parse string 's' into the 1st word 'first' and the remainder 'rest' }
  16. function trimLeftStr( s : string ) : string;
  17.    { remove leading separators from string 's' }
  18. function trimRightStr( s : string ) : string;
  19.    { remove trailing separators from string 's' }
  20. function trimEndsStr( s : string ) : string;
  21.    { trim separators from both left and right of string }
  22.  
  23.  
  24. implementation
  25. function removeLeadChars( s : string; chars : charSet ) : string;
  26.    { trims string 's' by removing chars in charSet 'chars'
  27.    e.g. charSet might be separators. So if s = ' .hello'
  28.    this function would return: 'hello'. }
  29. var
  30.    outputS : string;
  31.    i : integer;
  32. begin
  33.    outputS := s;
  34.    i := 1;
  35.        { count to end of characters found in charSet }
  36.    if not (outputS = '') then
  37.    begin
  38.      while (outputS[i] in chars) do
  39.        inc(i);
  40.      Delete(outputS,1, i-1);
  41.    end;
  42.    removeLeadChars := outputS;
  43. end;
  44.  
  45. function removeTrailingChars( s : string; chars : charSet ) : string;
  46. var
  47.    endOfS : integer;
  48.    cleanS : string;
  49. begin
  50.    cleanS := '';
  51.    endOfS := length(s);
  52.        { count backward from end of characters found in charSet }
  53.    while (s[endOfS] in chars) and (endOfS <> 0 ) do
  54.        dec(endOfS);
  55.        { then return a copy of the string minus the unwanted trailing chars }
  56.        { unless the string is empty in which case return it as such }
  57.    if endOfS <> 0 then cleanS := copy(s, 1, endOfS);
  58.    removeTrailingChars := cleanS;
  59. end;
  60.  
  61.  
  62. function trimLeftStr( s : string ) : string;
  63. { remove leading separators from string 's' }
  64. begin
  65.    trimLeftStr := removeLeadChars( s, separators );
  66. end;
  67.  
  68. function trimRightStr( s : string ) : string;
  69. { remove trailing separators from string 's' }
  70. begin
  71.    trimRightStr := removeTrailingChars( s, separators );
  72. end;
  73.  
  74. function trimEndsStr( s : string ) : string;
  75. { trim separators from both left and right of string }
  76. begin
  77.    trimEndsStr := trimLeftStr(trimRightStr(s));
  78. end;
  79.  
  80. procedure firstrestStr( var s, first, rest : string );
  81. { Given a string, 's', parse out the first word as 'first' and
  82.   leave the remainder of the string untouched as 'rest' }
  83. var
  84.    i : integer;
  85.    endOfWord : boolean;
  86.    trimmed_s : string;
  87. begin
  88.    endOfWord := false;
  89.    i := 1;
  90.    first := '';
  91.    rest := '';
  92.    trimmed_s := trimLeftStr( s );
  93.        { if s is a blank string then don't try to process it }
  94.    if length(trimmed_s) = 0 then endOfWord := true;
  95.        { when a a separator is found, endOfWord is true. So parse
  96.          out first word and rest of string. }
  97.    while not endOfWord do
  98.    begin
  99.        if trimmed_s[i] in separators then
  100.        begin
  101.            endOfWord := true;
  102.            first := copy( trimmed_s, 1, i-1 );
  103.            rest := copy(trimmed_s, i, (length(trimmed_s)-i)+1);
  104.        end
  105.        else inc(i);
  106.    end;
  107. end;
  108.  
  109. end.
  110.